<template>
    <el-container class="home-container">
        <el-header>
            <div class="el-header-div">
                <img src="../assets/img/touxiang.jpg" alt="" width="50px" height="50px"/>
                <span>电商后台管理系统</span>
            </div>
            <el-button type="info" @click="logout">退出</el-button>
        </el-header>
        <el-container>
            <el-aside :width="isCollapse?'64px':'200px'">
                <div class="toggle-button" @click="toggleCollapse">|||</div>
                <el-menu background-color="#333744" text-color="#fff" active-text-color="#409EFF" 
                    unique-opened :collapse='isCollapse' :collapse-transition="false" router :default-active="activePath">
                    <!-- 循环一级菜单 -->
                    <el-submenu :index="'/'+item.path" v-for="item in menulist" :key="item.id">
                        <template slot="title">
                            <i :class="iconObj[item.id]"></i>
                            <span>{{ item.authName }}</span>
                        </template>
                        <!-- 循环二级菜单 -->
                        <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/'+subItem.path)">
                            <template slot="title">
                                <i :class="iconObj[subItem.id]"></i>
                                <span>{{ subItem.authName }}</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <el-main>
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
export default {
    name: '',
    data(){
        return{
            menulist: [],
            iconObj:{
                '125': 'el-icon-s-custom',
                '103': 'el-icon-s-operation',
                '101': 'el-icon-s-shop',
                '102': 'el-icon-s-order',
                '145': 'el-icon-s-data',
                '110': 'el-icon-user',
                '111': 'el-icon-user-solid',
                '112': 'el-icon-circle-plus-outline',
                '104': 'el-icon-s-grid',
                '115': 'el-icon-discount',
                '121': 'el-icon-document-copy',
                '107': 'el-icon-s-management',
                '146': 'el-icon-s-marketing',
            },
            isCollapse:false,
            // 被激活的链接地址
            activePath: ''
        }
    },
    methods: {
        // 注销
        logout(){
            // 清除token
            window.sessionStorage.clear();
            // 重定向到登录页面
            this.$router.push('/login')
        },
        // 获取菜单列表
        async getMenuList(){
            const res = await this.$http.get('/menus')
            if(res.data.meta.status != 200){
                return this.$message.error(res.data.meta.status.msg)
            }
            this.menulist = res.data.data
        },
        // 控制菜单折叠
        toggleCollapse(){
            this.isCollapse = ! this.isCollapse
        },
        // 保存链接的激活状态
        saveNavState(activePath){
            window.sessionStorage.setItem('activePath',activePath)
            this.activePath = activePath
        }
        
    },
    created(){
        this.getMenuList()
        this.activePath = window.sessionStorage.getItem('activePath')
    }
}
</script>

<style scoped>
    .el-header{
        background-color: #373d41;
        display: flex;
        justify-content: space-between;
        align-items: center;
        color: #fff;
        font-size: 20px;
    }
    .el-header-div{
        display: flex;
        align-items: center;
    }
    .el-header-div span{
        margin-left: 15px;
    }
    .el-header img{
        border-radius: 50%;
    }
    .el-aside{
        background-color: #333744;
    }
    .el-main{
        background-color: #EAEDF1;
    }
    .home-container{
        height: 100%;
    }
    .el-menu{
        border: none;
    }
    .toggle-button{
        background-color: #4A5064;
        font-size: 10px;
        line-height: 24px;
        color: #fff;
        text-align: center;
        letter-spacing: 0.2em;
        cursor: pointer;
    }
</style>